package com.world.rickcloudendpoint.message;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @description: 网络基本消息报文
 * @author: jlsong
 * @date: 2021/12/22 20:30:26
 * @version: 1.0
 */
public abstract class AbstractNetMessage implements INetMessage {
    private NetMessageHead netMessageHead;
    private NetMessageBody netMessageBody;

    /**
     * 增加默认属性(附带逻辑调用需要的属性)
     */
    private final ConcurrentHashMap<Object, Object> attributes = new ConcurrentHashMap<>(16);

    @Override
    public NetMessageHead getNetMessageHead() {
        return netMessageHead;
    }

    @Override
    public NetMessageBody getNetMessageBody() {
        return netMessageBody;
    }

    public void setNetMessageHead(NetMessageHead netMessageHead) {
        this.netMessageHead = netMessageHead;
    }

    public void setNetMessageBody(NetMessageBody netMessageBody) {
        this.netMessageBody = netMessageBody;
    }

    /**
     * 逻辑处理时附带的参数
     *
     * @param key   关键字
     * @param value 值
     * @return 值
     */
    public Object setAttribute(Object key, Object value) {
        return attributes.put(key, value);
    }

    public Object getAttribute(Object key){
        return attributes.get(key);
    }

    public void removeAttribute(Object key){
        this.attributes.remove(key);
    }


    public int getSerial(){
        return netMessageHead.getSerial();
    }

    public int getCmd(){
        return getNetMessageHead().getCmd();
    }
}
